package com.smiier.skin;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;
import cn.o.app.json.JsonUtil;
import cn.o.app.net.INetTask;
import cn.o.app.net.NetTaskListener;
import cn.skinapp.R;

import com.evan.common.adapter.BaseListAdapter;
import com.evan.common.pullrefresh.PullToRefreshBase;
import com.evan.common.pullrefresh.PullToRefreshBase.OnRefreshListener;
import com.evan.common.pullrefresh.PullToRefreshListView;
import com.smiier.skin.constant.Constant;
import com.smiier.skin.net.AnswerAppraiseGetListTask;
import com.smiier.skin.net.AnswerAppraiseGetListTask.AnswerAppraiseGetListRequest;
import com.smiier.skin.net.AnswerAppraiseGetListTask.AnswerAppraiseGetListResponse;
import com.smiier.skin.ui.BasicActivity;
import com.smiier.skin.utils.CommonUtility;

/**
 * @{# CommentActivity.java Create on 2014-7-1 下午8:54:12
 * 
 * @author <a href="mailto:evan0502@qq.com">Evan</a>
 * @version 1.0
 * 
 *          评价列表
 */

public class CommentActivity extends BasicActivity implements
		OnRefreshListener<ListView> {

	private JSONObject doctor;

	private PullToRefreshListView list_comment;

	CommentAdapter adapter;
	String startid;
	ArrayList<Object> array = new ArrayList<Object>();
	private int mLevel;
	private long mDoctorUid;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_comment);
		init();
		setNavTitle("我的评价");
		list_comment = (PullToRefreshListView) findViewById(R.id.list_comment);
		list_comment.setPullLoadEnabled(false);
		list_comment.setPullRefreshEnabled(false);
		list_comment.setScrollLoadEnabled(true);
		list_comment.setOnRefreshListener(this);

		list_comment.doPullRefreshing(true);
		list_comment.hideDivider();
		try {
			doctor = new JSONObject(getIntent().getStringExtra(
					Constant.IDENTITY_KEY));
			mDoctorUid = Long.parseLong(doctor.getString("Uid"));
		} catch (Exception e) {

			e.printStackTrace();
		}

		mLevel = getIntent().getIntExtra(Constant.IDENTITY_KEY_1, -1);

	}

	private void loadData() {
		AnswerAppraiseGetListTask task = new AnswerAppraiseGetListTask();
		AnswerAppraiseGetListRequest req = new AnswerAppraiseGetListRequest();
		if (mLevel != -1) {
			ArrayList<Integer> levels = new ArrayList<Integer>();
			levels.add(mLevel);
			req.Levels = levels;
		}
		req.doctor_uid = mDoctorUid;
		req.pagesize = Constant.pageSize;
		req.startid = startid;
		task.setRequest(req);
		task.addListener(new NetTaskListener<AnswerAppraiseGetListRequest, AnswerAppraiseGetListResponse>() {
			@Override
			public void onException(
					INetTask<AnswerAppraiseGetListRequest, AnswerAppraiseGetListResponse> task,
					Exception e) {
			}

			@Override
			public void onComplete(
					INetTask<AnswerAppraiseGetListRequest, AnswerAppraiseGetListResponse> task,
					AnswerAppraiseGetListResponse response) {

				if (response.ResultCode != 200) {
					list_comment.setContextEmptyType(true, 2);
					return;
				}

				list_comment.doComplete();
				try {
					JSONArray arr = JsonUtil
							.convertToArray(response.Res.Appraises);
					if (arr.length() > 0) {
						CommonUtility.putAll(array, arr, true);
						list_comment.setHasMoreData(true);
					} else {
						list_comment.setHasMoreData(false);
						list_comment.getLoadingLayout().setHintText(null);
					}
					if (array.size() > 0) {
						startid = ((JSONObject) array.get(array.size() - 1))
								.getString("id");
					} else {
						list_comment.getLoadingLayout().setHintText("暂无评价记录");
					}
					if (CommonUtility.isNull(adapter)) {
						adapter = new CommentAdapter((Activity) getContext(),
								array);
						list_comment.setAdapter(adapter);
					} else {
						adapter.notifyUpdate();
					}
					if (array.size() < 1) {
						list_comment.setContextEmptyType(true, 2);
					} else {
						list_comment.setContextEmptyType(false, 2);
					}

				} catch (Exception e) {
					list_comment.setContextEmptyType(true, 2);
					e.printStackTrace();
				}

			}
		});
		add(task);
	}

	@Override
	public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
		startid = "";
		array.clear();
		loadData();
	}

	@Override
	public void onPullUpToLoadMore(PullToRefreshBase<ListView> refreshView) {
		loadData();
	}

	public static class CommentAdapter extends BaseListAdapter {

		public CommentAdapter(Activity activity, ArrayList<Object> array) {
			super(activity, array);
		}

		class ViewHolder {
			TextView text_comment_content, text_comment_date;
			TextView btn_comment_type;
			TextView mCommentName;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder viewHolder = null;
			if (CommonUtility.isNull(convertView)) {
				convertView = inflater.inflate(R.layout.item_comment, null);
				viewHolder = new ViewHolder();
				viewHolder.text_comment_content = (TextView) convertView
						.findViewById(R.id.text_comment_content);
				viewHolder.text_comment_date = (TextView) convertView
						.findViewById(R.id.text_comment_date);
				viewHolder.btn_comment_type = (TextView) convertView
						.findViewById(R.id.image_comment_type);
				viewHolder.mCommentName = (TextView) convertView
						.findViewById(R.id.comment_name);
				CommonUtility.setViewHolderTag(convertView, viewHolder);
			} else {
				viewHolder = (ViewHolder) CommonUtility
						.getViewHolderTag(convertView);
			}

			JSONObject object = (JSONObject) getItem(position);
			try {
				viewHolder.text_comment_content.setText(object
						.getString("Content"));
				viewHolder.text_comment_date.setText(CommonUtility.formatDate(
						object.getString("CreateTime"), 0, 10));

				int level = object.getInt("Level");

				if (level == 1) {
					/*
					 * viewHolder.btn_comment_type
					 * .setBackgroundResource(R.drawable.tag_comment_bad);
					 * viewHolder.btn_comment_type.setText("差评");
					 * viewHolder.btn_comment_type.setTextColor(0xff858585);
					 */
				} else if (level == 2) {
					/*
					 * viewHolder.btn_comment_type
					 * .setBackgroundResource(R.drawable.tag_comment_middle);
					 * viewHolder.btn_comment_type.setText("中评");
					 * viewHolder.btn_comment_type.setTextColor(0xffffc02a);
					 */
				} else if (level == 3) {
					/*
					 * viewHolder.btn_comment_type
					 * .setBackgroundResource(R.drawable.tag_comment_good);
					 */
					viewHolder.btn_comment_type.setText("好评");
					// viewHolder.btn_comment_type.setTextColor(0xff91d2b9);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			// CommonUtility.setListViewItemBG(convertView, position);
			return convertView;
		}

	}

	/*
	 * @Override public void onResume() { super.onResume();
	 * MobclickAgent.onResume(getContext()); }
	 * 
	 * @Override public void onPause() { super.onPause();
	 * MobclickAgent.onPause(getContext()); }
	 */
}
